package com.dimanche.library_base.baseui;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.Nullable;

import com.dimanche.library_base.utils.InputTools;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.toast.ToastUtils;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;


/**
 * author : Dimanche
 * date   : 2020/12/28 21:24
 * desc   :
 * version: 1.0
 */
public abstract class BaseActivity extends RxAppCompatActivity implements IBaseActivity {
    private static ProgressDialog mProgressDialog;//加载对话框
    public RxAppCompatActivity mActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        initLayout(savedInstanceState);
        initViews();
    }


    protected void initLayout(Bundle savedInstanceState) {
        if (getLayoutId(savedInstanceState) > 0) {
            setContentView(getLayoutId(savedInstanceState));
        }
    }

    /**
     * 初始化根布局
     *
     * @return 布局layout的id
     */
    @Override
    public int getLayoutId(Bundle savedInstanceState) {
        return 0;
    }

    @Override
    public void initViews() {

    }

    /**
     * 与setContextView对应
     *
     * @return
     */
    @Override
    public ViewGroup getContentView() {
        return findViewById(Window.ID_ANDROID_CONTENT);
    }

    @Override
    public RxAppCompatActivity getActivity() {
        return mActivity;
    }

    @Override
    public void openImmersive() {
        ImmersionBar.with(mActivity).init();
    }


    @Override
    protected void onPause() {
        super.onPause();
        InputTools.hideInputMethod(mActivity);
        dissLoading();
    }


    @Override
    public void showLoading() {
        showLoading("加载中~");
    }

    @Override
    public void showLoading(String msg) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(mActivity);
            mProgressDialog.setCancelable(false);
        }
        mProgressDialog.setMessage(msg);
        //避免页面上有多个框同时显示
        if (!mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }
    }

    @Override
    public void dissLoading() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void showSuccess(String msg) {
        ToastUtils.show(msg);
    }

    @Override
    public void showError(String msg) {
        ToastUtils.show(msg);
    }


}
